import React from 'react';
import { Upload, Button, message } from 'antd';

export default class DealerUpload extends React.Component {
  state = {
    fileList: [],
  }
  handleChange = (info) => {
    let fileList = info.fileList;
    const { setVisible, setListData } = this.props;
    // 1. Limit the number of uploaded files
    //    Only to show two recent uploaded files, and old ones will be replaced by the new
    fileList = fileList.slice(-1);

    // 2. read from response and show file link
    // fileList = fileList.map((file) => {
    //   if (file.response) {
    //     // Component will show file.url as link
    //     file.url = file.response.url;
    //   }
    //   return file;
    // });

    // 3. filter successfully uploaded files according to response from server
    fileList = fileList.filter((file) => {
      if (file.response) {
        if (file.response.error !== 0) {
          message.error(file.response.message);
          setVisible(false);
          return false;
        } else {
          setVisible(true);
          setListData(file.response.list);
          return true;
        }
      }
      return true;
    });
    this.setState({ fileList });
  }
  render() {
    const { style, data } = this.props;
    const domain = window.g.url;
    const url = '/common/uploaddealer';
    const str = `${domain}${url}`;
    const props = {
      action: str,
      onChange: this.handleChange,
      multiple: false,
    };
    return (
      <Upload
        {...props}
        fileList={this.state.fileList}
        showUploadList={false}
        style={style}
        data={data}
      >
        <Button type="primary" >
           导入
        </Button>
      </Upload>
    );
  }
}

